package com.learning.mymedia;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        Button recordTestButton = findViewById(R.id.btn_record_test);
        recordTestButton.setOnClickListener(this);
        Button videoTestButton = findViewById(R.id.btn_video_test);
        videoTestButton.setOnClickListener(this);



    }

    @RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_record_test) {
            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_MEDIA_AUDIO) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_MEDIA_AUDIO, Manifest.permission.RECORD_AUDIO}, 1);
            } else {
                Intent intent = new Intent(MainActivity.this,MediaTest.class);
                startActivity(intent);
            }

        } else if (v.getId() == R.id.btn_video_test) {
            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_MEDIA_VIDEO) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_MEDIA_VIDEO}, 2);
            } else {
                Intent intent = new Intent(MainActivity.this,VideoTest.class);
                startActivity(intent);
            }

        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 用户授予了权限
                Toast.makeText(this, "yes", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(this,MediaTest.class);
                startActivity(intent);
            } else {
                // 用户拒绝了权限，显示提示
                Toast.makeText(this, "永久拒绝了权限，请前往系统设置开启权限", Toast.LENGTH_SHORT).show();
            }
        } else if (requestCode == 2) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 用户授予了权限
                Toast.makeText(this, "yes", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(this,VideoTest.class);
                startActivity(intent);
            } else {
                // 用户拒绝了权限，显示提示
                Toast.makeText(this, "永久拒绝了权限，请前往系统设置开启权限", Toast.LENGTH_SHORT).show();
            }
        }
    }
}